package com.playfair.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.playfair.core.AppCore;

public class EnDecryptDialog extends JFrame {

	private static final long serialVersionUID = -8104973694321258190L;

	private JLabel keyLabel, textLabel, codeLabel, interchangeLabel,
			endLetterLabel;
	private JTextArea keyTextArea, textTextArea, codeTextArea;
	private JButton confirmButton, cancelButton, switchLetters;
	private JScrollPane keyScrollPane, textScrollPane, codeScrollPane;
	private JComboBox<Character> interchangeComboBox1, interchangeComboBox2,
			endLetterComboBox;
	private JPanel interchangePanel, letterPanelLeft, letterPanelRight,
			endLetterPanel;
	private Vector<Character> characters, chars1, chars2, charsEnd;
	private DefaultComboBoxModel<Character> endCharModel, inter1, inter2;

	private boolean encrypt;

	public EnDecryptDialog(boolean encrypt) {

		characters = new Vector<Character>();
		for (Character i = 'A'; i <= 'Z'; i++)
			characters.add(i);

		this.encrypt = encrypt;
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle(encrypt ? "Encrypt" : "Decrypt");
		setLayout(new GridLayout(5, 2));
		setContent();
		pack();
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension dim = kit.getScreenSize();
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
		setResizable(false);
	}

	private void setContent() {
		keyLabel = new JLabel("Key");
		textLabel = new JLabel("Text");
		codeLabel = new JLabel("Code");
		interchangeLabel = new JLabel("Interchangeable letters");
		endLetterLabel = new JLabel("End letter");
		cancelButton = new JButton("Cancel");
		confirmButton = new JButton(encrypt ? "Encrypt" : "Decrypt");

		letterPanelLeft = new JPanel();
		letterPanelLeft.setLayout(new GridLayout(2, 1));
		letterPanelLeft.add(interchangeLabel);
		letterPanelLeft.add(endLetterLabel);

		chars1 = new Vector<Character>(characters);
		chars2 = new Vector<Character>(characters);
		charsEnd = new Vector<Character>(characters);

		endCharModel = new DefaultComboBoxModel<>(charsEnd);
		inter1 = new DefaultComboBoxModel<>(chars1);
		inter2 = new DefaultComboBoxModel<>(chars2);
		interchangeComboBox1 = new JComboBox<Character>(inter1);
		inter1.removeElement(AppCore.getInstance().getInterchangeChar2());
		interchangeComboBox2 = new JComboBox<Character>(inter2);
		inter2.removeElement(AppCore.getInstance().getInterchangeChar1());
		endLetterComboBox = new JComboBox<Character>(endCharModel);
		interchangeComboBox1.setSelectedItem(AppCore.getInstance()
				.getInterchangeChar1());

		interchangeComboBox2.setSelectedItem(AppCore.getInstance()
				.getInterchangeChar2());
		endLetterComboBox.setSelectedItem(AppCore.getInstance().getFinalChar());

		endLetterComboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				AppCore.getInstance().setFinalChar(
						(Character) getEndLetterComboBox().getSelectedItem());
			}
		});

		interchangeComboBox1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				AppCore.getInstance()
						.setInterchangeChar1(
								(Character) getInterchangeComboBox1()
										.getSelectedItem());

				setChars1(new Vector<Character>(getCharacters()));
				setInter2(new DefaultComboBoxModel<>(getChars1()));
				getInterchangeComboBox2().setModel(getInter2());
				getInterchangeComboBox2().setSelectedItem(
						AppCore.getInstance().getInterchangeChar2());
				getInter2().removeElement(
						AppCore.getInstance().getInterchangeChar1());
			}
		});

		interchangeComboBox2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				AppCore.getInstance()
						.setInterchangeChar2(
								(Character) getInterchangeComboBox2()
										.getSelectedItem());

				setChars2(new Vector<Character>(getCharacters()));
				setInter1(new DefaultComboBoxModel<>(getChars2()));
				getInterchangeComboBox1().setModel(getInter1());
				getInterchangeComboBox1().setSelectedItem(
						AppCore.getInstance().getInterchangeChar1());
				getInter1().removeElement(
						AppCore.getInstance().getInterchangeChar2());

			}
		});

		switchLetters = new JButton("Switch");

		switchLetters.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Character swp = AppCore.getInstance().getInterchangeChar1();
				AppCore.getInstance().setInterchangeChar1(
						AppCore.getInstance().getInterchangeChar2());
				AppCore.getInstance().setInterchangeChar2(swp);

				setChars2(new Vector<Character>(getCharacters()));
				setInter1(new DefaultComboBoxModel<>(getChars2()));
				getInterchangeComboBox1().setModel(getInter1());
				getInterchangeComboBox1().setSelectedItem(
						AppCore.getInstance().getInterchangeChar1());
				getInter1().removeElement(
						AppCore.getInstance().getInterchangeChar2());

				setChars1(new Vector<Character>(getCharacters()));
				setInter2(new DefaultComboBoxModel<>(getChars1()));
				getInterchangeComboBox2().setModel(getInter2());
				getInterchangeComboBox2().setSelectedItem(
						AppCore.getInstance().getInterchangeChar2());
				getInter2().removeElement(
						AppCore.getInstance().getInterchangeChar1());
			}
		});

		interchangePanel = new JPanel();
		interchangePanel.add(interchangeComboBox1);
		interchangePanel.add(interchangeComboBox2);
		interchangePanel.add(switchLetters);
		letterPanelRight = new JPanel();
		letterPanelRight.setLayout(new GridLayout(2, 1));
		letterPanelRight.add(interchangePanel);
		endLetterPanel = new JPanel();
		endLetterPanel.add(endLetterComboBox);
		letterPanelRight.add(endLetterPanel);

		keyTextArea = new JTextArea(5, 10);
		keyTextArea.setLineWrap(false);
		textTextArea = new JTextArea(5, 10);
		textTextArea.setLineWrap(true);
		codeTextArea = new JTextArea(5, 10);
		codeTextArea.setLineWrap(true);

		keyScrollPane = new JScrollPane(keyTextArea);
		keyScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		textScrollPane = new JScrollPane(textTextArea);
		textScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		codeScrollPane = new JScrollPane(codeTextArea);
		codeScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});

		confirmButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (encrypt) {
					codeTextArea.setText(AppCore.getInstance().encrypt(
							keyTextArea.getText().trim(),
							textTextArea.getText().trim()));
					codeTextArea.setEnabled(true);
				} else {
					textTextArea.setText(AppCore.getInstance().decrypt(
							keyTextArea.getText().trim(),
							codeTextArea.getText().trim()));
					textTextArea.setEditable(true);
				}
			}
		});
		add(letterPanelLeft);
		add(letterPanelRight);
		add(keyLabel);
		add(keyScrollPane);
		
		endLetterComboBox.setEnabled(false);
		interchangeComboBox1.setEnabled(false);
		interchangeComboBox2.setEnabled(false);
		switchLetters.setEnabled(false);
		
		if (encrypt) {
			add(textLabel);
			add(textScrollPane);
			add(codeLabel);
			add(codeScrollPane);
			codeTextArea.setEnabled(false);
			codeTextArea.setBackground(Color.LIGHT_GRAY);
		} else {
			add(codeLabel);
			add(codeScrollPane);
			add(textLabel);
			add(textScrollPane);
			textTextArea.setEditable(false);
			textTextArea.setBackground(Color.LIGHT_GRAY);
			endLetterComboBox.setEnabled(false);
		}
		add(confirmButton);
		add(cancelButton);
	}

	public JComboBox<Character> getInterchangeComboBox1() {
		return interchangeComboBox1;
	}

	public JComboBox<Character> getInterchangeComboBox2() {
		return interchangeComboBox2;
	}

	public JComboBox<Character> getEndLetterComboBox() {
		return endLetterComboBox;
	}

	public Vector<Character> getChars1() {
		return chars1;
	}

	public Vector<Character> getChars2() {
		return chars2;
	}

	public Vector<Character> getCharsEnd() {
		return charsEnd;
	}

	public Vector<Character> getCharacters() {
		return characters;
	}

	public DefaultComboBoxModel<Character> getInter1() {
		return inter1;
	}

	public void setInter1(DefaultComboBoxModel<Character> inter1) {
		this.inter1 = inter1;
	}

	public DefaultComboBoxModel<Character> getInter2() {
		return inter2;
	}

	public void setInter2(DefaultComboBoxModel<Character> inter2) {
		this.inter2 = inter2;
	}

	public void setChars1(Vector<Character> chars1) {
		this.chars1 = chars1;
	}

	public void setChars2(Vector<Character> chars2) {
		this.chars2 = chars2;
	}

	public void setCharsEnd(Vector<Character> charsEnd) {
		this.charsEnd = charsEnd;
	}

}
